package cn.edu.yzu.scholarship.controller;

import java.util.List;
import java.util.Map;

import cn.edu.yzu.scholarship.kit.ExcelKit;
import cn.edu.yzu.scholarship.model.Student;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;

public class ImportCtrler extends Controller {

	public void impfile() {
		try {
			setSessionAttr("importData", ExcelKit.getData(getFile().getFile()));
		} catch (Exception e) {
			e.printStackTrace();
			renderJson("errorInfo","读取文件出现错误，请检查文件内容是否正确！");
			return;
		}
		renderJson("errorInfo","none");
	}
	
	@SuppressWarnings("unchecked")
	public void validate() {
		Object result = Student.validate((List<String[]>) getSessionAttr("importData"));
		removeSessionAttr("importData");
		renderJson(result);
	}
	
	public void template() {
		renderFile("/statics/xls/student_import_template.xls");
	}
	
	@SuppressWarnings("unchecked")
	public void importData() {
		Student.importData((List<Map<String,Object>>)JSON.parseObject(getPara("data"),List.class));
		renderJson("errorInfo","none");
	}
}
